package com.ct.gd.passport.view.consult;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.BaseActivity;
import com.ct.gd.passport.common.util.StringUtils;
import com.ct.gd.passport.domain.AdviceForm;
import com.ct.gd.passport.domain.AdviceInfo;
import com.ct.gd.passport.util.TelephonyState;
import com.ct.gd.passport.view.BundleKeys;
import com.ct.gd.passport.view.widget.CustomHeaderFragment;
import com.umeng.analytics.MobclickAgent;
import com.up91.common.android.async.ILoading;
import com.up91.common.android.async.SimpleAsyncTask;
import com.up91.common.android.helper.ToastHelper;

public class SubmitActivity extends BaseActivity
        implements
            OnClickListener {

    private TableRow mSpinnerRow;
    /**类型*/
    private Spinner    mSpinnerCategory;
    /**联系方式*/
    private EditText   mEdtContact;
    /**联系人*/
    private EditText   mEdtSubmitter;
    private TextView   mTvAdviceTitle;
    private EditText   mEdAdviceContent;
    private AdviceForm mAdviceForm;

    // private ConsultCatalogFragment mFgConsultCatalog;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.consult_submit);
    }

    @Override
    protected void initViews() {
        CustomHeaderFragment header = (CustomHeaderFragment) getFragmentManager().findFragmentById(R.id.header);
        header.setCenterText(R.string.advice_submit_intro);

        mEdAdviceContent = (EditText) findViewById(R.id.ed_advice_content);
        findViewById(R.id.btn_advice_submit).setOnClickListener(this);
        mAdviceForm = (AdviceForm) getIntent().getSerializableExtra(BundleKeys.ADVICE_FORM);
        // mEdAdviceContent.setHint(mAdviceForm.tip);

        mTvAdviceTitle = (TextView) findViewById(R.id.tv_advice_title);
        mTvAdviceTitle.setText(mAdviceForm.tip);

        mSpinnerRow = (TableRow)findViewById(R.id.spinner_row);
        if(mAdviceForm == AdviceForm.QT){
            mSpinnerRow.setVisibility(View.VISIBLE);
        }
        mSpinnerCategory = (Spinner)findViewById(R.id.spinner_category);
        mEdtContact = (EditText)findViewById(R.id.edt_contact);
        try {
            String phone = TelephonyState.getInstance().getPhoneNumber();
            if(org.apache.commons.lang3.StringUtils.isNotEmpty(phone)){
                mEdtContact.setText(phone);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mEdtSubmitter = (EditText)findViewById(R.id.edt_submitter);
        
        ArrayAdapter<CharSequence> mSpinnerCategoryAdapter = ArrayAdapter.createFromResource(this, R.array.advice_submit_category,
                android.R.layout.simple_spinner_item);
        mSpinnerCategoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinnerCategory.setAdapter(mSpinnerCategoryAdapter);
        
        // mFgConsultCatalog = (ConsultCatalogFragment)
        // mFgMan.findFragmentById(R.id.area_consult_history);
    }

    //
    // @Override
    // protected void onAfterCreate(Bundle savedInstanceStat) {
    // super.onAfterCreate(savedInstanceStat);
    // // mFgConsultTag.registerOnRootChangedListener(mFgConsultCatalog);
    // mFgConsultCatalog.loadCatalog();
    // }

    @Override
    public void onClick(View v) {
        String content = mEdAdviceContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastHelper.toast(this, R.string.advice_submit_not_empty);
        } else {
            MobclickAgent.onEvent(this, mAdviceForm.submit_event_id);
            String title = mAdviceForm.name+"-" + "咨询信息";
            if(mAdviceForm == AdviceForm.QT){
                title = mAdviceForm.name +"-"+mSpinnerCategory.getSelectedItem()+"-" + "咨询信息";
            }
            new SubmitTask(this, true).execute(mAdviceForm.serial+"",title,content,mEdtSubmitter.getText().toString(),mEdtContact.getText().toString());
        }
    }

    private class SubmitTask extends SimpleAsyncTask<String, Void, Boolean> {
        public SubmitTask(ILoading loading, boolean showLoading) {
            super(loading, showLoading);
        }

        @Override
        protected Boolean onLoad(String... params)
                throws Exception {
            String type = params[0];
            String title = params[1];
            String content = params[2];
            String contact = params[3];
            String contactPhone = params[4];
            return AdviceInfo.submit(type,title,content,contact,contactPhone);
        }

        @Override
        protected void onUI(Boolean result) {
            if (result) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(SubmitActivity.this).setTitle(R.string.success).setMessage(mAdviceForm.success);
                dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SubmitActivity.this.finish();
                    }
                });
                dialog.show();
                // ToastHelper.toast(SubmitActivity.this, mAdviceForm.success);
            }
        }
    }
}
